﻿using Assignment5;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form2 : Form
    {
        List<Goods> goodsList;
        List<Customer> customerList;
        Customer customer;
        List<Goods> selectedGoods;
        string goodString;

        public Form2(List<Goods> g, List<Customer> c)
        {
            InitializeComponent();
            goodsList = g;
            customerList = c;
            goodString = null;
            selectedGoods = new List<Goods>();
            this.Load += Form2_Load;
        }

        //将客户列表和商品列表分别设置为数据绑定源
        public void Form2_Load(object sender, EventArgs args)
        {
            customerBindingSource.DataSource = customerList;
            goodsBindingSource.DataSource = goodsList;
        }

       //选择需要添加的客户信息
        private void cusTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string text = null;
            if (e.RowIndex >= 0)
            {
                DataGridViewRow doubleClickedRow = cusTable.Rows[e.RowIndex];
                int cusID = Convert.ToInt32(doubleClickedRow.Cells["cusIdCol"].Value);
                text = doubleClickedRow.Cells["cusNameCol"].Value.ToString();
                text += "(id" + doubleClickedRow.Cells["cusIdCol"].Value.ToString() + ")";
                customer = customerList.FirstOrDefault(c => cusID == c.CustomerId);
            }
            if (text != null)
            {
                cusSelected.ForeColor = Color.Black;
                cusSelected.Text = text;
            }
        }

        //选择需要添加的商品
        private void goodTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow doubleClickedRow = goodTable.Rows[e.RowIndex];
                int goID = Convert.ToInt32(doubleClickedRow.Cells["GoodIdCol"].Value);
                goodString += doubleClickedRow.Cells["GoodNameCol"].Value.ToString();
                goodString += "(id" + doubleClickedRow.Cells["GoodIdCol"].Value.ToString()+")";
                Goods g=goodsList.FirstOrDefault(c => c.GoodId== goID);
                selectedGoods.Add(g);
            }
            if (goodString != null)
            {
                goodSelected.ForeColor = Color.Black;
                goodSelected.Text = goodString;
            }
        }

        //确认添加
        private void confirm_Click(object sender, EventArgs e)
        {
            Order newOrder = new Order(customer);
            selectedGoods.ForEach(newOrder.addGoods);
            OrderService orderService=OrderService.getInstance();
            orderService.addOrder(newOrder);
            MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }
        private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }
    }
}
